/**
 * Created by shudong.msd(风骐) on 2016/4/27.
 */
app.factory('indexKeywordsService',['$http','$q','$state',function($http,$q,$state){

    var comutil = new commUtils();

    var service = {};

    service.loadWords = function(type,query){
        var deferred = $q.defer();

        $http.get(UrlMappings.INDEX_TV_VARIETY_KEYWORD_SEARCH(type),{
            params : {
                'access_token':comutil.getTocken(),
                'q':query
            },
            headers: {
                'Accept':'application/json',
                'Content-Type': 'application/x-www-form-urlencoded'
            }
        }).success(function (data) {
            if (data.length==0){
                if(type=='tv') data = [{"id":-1, "name": "无此电视剧", "area": "", "year": "" }];
                else data = [{"id":-1, "name": "无此综艺节目", "area": "", "year": "" }];
            }
            deferred.resolve(data)
        }).error(function(data,status) {
            if(status=='401')comutil.logout($state);
            deferred.reject(data);
        });
        return deferred.promise;
    };


    service.loadStars = function(query){
        var deferred = $q.defer();

        $http.get(UrlMappings.INDEX_STAR_KEYWORD_SEARCH,{
            params : {
                'access_token':comutil.getTocken(),
                'q':query
            },
            headers: {
                'Accept':'application/json',
                'Content-Type': 'application/x-www-form-urlencoded'
            }
        }).success(function (data) {
            if (data.length==0) data = [{"id":-1, "name": "无此明星", "category": ""}];
            deferred.resolve(data)
        }).error(function(data,status) {
            if(status=='401')comutil.logout($state);
            deferred.reject(data);
        });
        return deferred.promise;
    };


    return service;

}]);